# 这是频道种类定位的模块
from common.base import Base, By
from common.functions import browser_operation


class CategoryPage(Base):
    """封装表现层: 制作定位器"""
    # 定位页面中所有的商品名称的位置
    goods_list_locator = (By.CSS_SELECTOR, '.goods-title>a')
    # 定位页面中显示商品总数目的位置
    goods_total_locator = (By.CSS_SELECTOR, 'td.tdl>b')

    def get_goods_total_text(self):
        # 调用类属性获取商品总数目的文本
        return self.get_element_text(self.goods_total_locator)

    def get_goods_title(self):
        # 调用类属性获取所有商品名称的元素
        goods = self.find_elements(self.goods_list_locator)
        # 列表表达式获取每个商品名称元素的title属性值, 并返回
        return [element.get_attribute('title') for element in goods]

    def click_goods(self, title):
        """点击所有的商品名称元素"""
        # 通过商品名称元素的title属性定位单个商品
        goods_locator = (By.CSS_SELECTOR, f'a[title="{title}"]')
        # 点击商品
        self.click(locator=goods_locator)


if __name__ == '__main__':
    driver = browser_operation()
    category = CategoryPage(driver)
    category.get_website('http://192.168.31.163/ecshop/category.php?id=1')
    print(category.get_goods_title())
